package com.sinosoft.online.web;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Context;
import org.apache.catalina.Manager;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.RequestFacade;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("session")
public class SessionController{

	@RequestMapping(value="getActiveSession", method=RequestMethod.GET)
	@ResponseBody
	public String getActiveSession(HttpServletRequest request, HttpSession session) throws Exception {
		int activeSessions = 0;
		//RequestFacade request = new RequestFacade(null);
	    if (request instanceof RequestFacade) {
	      Field requestField = request.getClass().getDeclaredField("request");
	      requestField.setAccessible(true);
	      Request req = (Request) requestField.get(request);
	      Context context = req.getContext();
	      Manager manager = context.getManager();
	      activeSessions = manager.getActiveSessions();
	    }
	    return activeSessions+"";
	}
}
